package com.example.smartgreenhouse.WebSocketServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class SoilMoistureWebSocketServer {
    private final SimpMessagingTemplate messagingTemplate;

    @Autowired
    public SoilMoistureWebSocketServer(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    public void sendSoilMoistureData(int soilMoisture,int changeValue,float changeRate,LocalDateTime collectTime){
        Map<String, Object> message = new HashMap<>();
        message.put("soil_moisture", soilMoisture);
        message.put("soil_moisture_change_value", changeValue);
        message.put("soil_moisture_change_rate", changeRate);
        message.put("collect_time", collectTime);
        messagingTemplate.convertAndSend("/topic/soil_moisture", message);
    }
}

